const columnServ = require('../services/columnService')
const { asyncHandler } = require('../routes/getSendResult')

// 获取所有专栏列表
exports.getColumns = asyncHandler(async (req, res, next) => {
  return await columnServ.getColumns()
})

// 获取专栏内的文章
exports.getColumn = asyncHandler(async (req, res, next) => {
  const { page, limit } = req.query
  const result = await columnServ.getColumnById(req.params.columnId)
  // 关联表分类查询不知道怎么做，所以就自己手写
  let begin = undefined
  let count = limit
  begin = (page - 1) * limit
  if (!page && !limit) {
    return result
  } else {
    result.dataValues.list = result.dataValues.list.splice(begin, count)
  }

  return result
})

// 创建专栏
exports.createColumn = asyncHandler(async (req) => {
  return await columnServ.createColumn(req.body)
})

// 更新专栏
exports.updateColumn = asyncHandler(async (req) => {
  return await columnServ.updateColumn(req.params.columnId, req.body)
})

// 删除专栏
exports.deleteColumn = asyncHandler(async (req) => {
  return await columnServ.deleteColumn(req.params.columnId)
})

exports.deleteColumnConnectArticle = asyncHandler(async (req) => {
  return await columnServ.deleteColumnConnectArticle(req.params.columnId, req.params.articleId)
})
